<template>
  <div class="app-container">


    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="测站名称"  prop="statCd">
            <el-select style="width: 280px" v-model="queryParams.statCd" filterable placeholder="测站名称" size="small" >
                <el-option v-for="(item,index) in czlist" :key="index" 
                    :label="item.statNm" :value="item.statCd"></el-option>
            </el-select>
        </el-form-item>
      <!-- <el-form-item label="即时水位" prop="curLevel">
        <el-input
          v-model="queryParams.curLevel"
          placeholder="请输入即时水位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="瞬时流量" prop="curFlow">
        <el-input
          v-model="queryParams.curFlow"
          placeholder="请输入瞬时流量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="累计流量" prop="sumFlow">
        <el-input
          v-model="queryParams.sumFlow"
          placeholder="请输入累计流量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="观测人员" prop="opeMan">
        <el-input
          v-model="queryParams.opeMan"
          placeholder="请输入观测人员"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="记录时间" prop="putTime">
        <el-date-picker clearable
          v-model="queryParams.putTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择记录时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="记录人" prop="putMan">
        <el-input
          v-model="queryParams.putMan"
          placeholder="请输入记录人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="备注" prop="statMark">
        <el-input
          v-model="queryParams.statMark"
          placeholder="请输入备注"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="日期" prop="chkTime">
        <el-date-picker v-model="queryParams.chkTime" size="small" value-format="yyyy-MM-dd"
                        type="date" placeholder="请选择（日）" >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['waterManagement:StManstatR:add']" >新增</el-button>
      </el-form-item>
    </el-form>

    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['waterManagement:StManstatR:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['waterManagement:StManstatR:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['waterManagement:StManstatR:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['waterManagement:StManstatR:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row> -->

    <el-table v-loading="loading" :data="StManstatRList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="测站代码" align="center" prop="statCd" /> -->
      <el-table-column label="序号" type="index" align="center" width="50" />
      <el-table-column label="即时水位" align="center" prop="curLevel">
      </el-table-column>
      <el-table-column label="瞬时流量" align="center" prop="curFlow">
      </el-table-column>
      <el-table-column label="累计流量" align="center" prop="sumFlow">
      </el-table-column>
      <el-table-column label="观测人员" align="center" prop="opeMan">
      </el-table-column>
      <el-table-column label="观测时间" align="center" prop="chkTime" width="180" />
      <el-table-column label="记录时间" align="center" prop="putTime" width="180">
      </el-table-column>
      <el-table-column label="记录人" align="center" prop="putMan">
      </el-table-column>
      <!-- <el-table-column label="备注" align="center" prop="statMark">
      </el-table-column> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['waterManagement:StManstatR:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['waterManagement:StManstatR:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改水情人工观测站数据记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="680px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-row v-if="btntype=='add'" >
          <el-form-item label="日期" prop="dt">
            <el-date-picker v-model="form.dt" size="small" value-format="yyyy-MM-dd"
                type="date" @change="gettime" placeholder="请选择（日）" >
            </el-date-picker>
          </el-form-item>
          <el-col :span="12">
              <el-form-item label="测站" prop="statCd"  >
                  <el-select v-model="form.statCd" placeholder="请选择测站" filterable size="small" @change="gettime" style="width: 100%;" >
                      <el-option v-for="dict in czlist" :key="dict.statCd"
                          :label="dict.statNm" :value="dict.statCd" />
                  </el-select>
              </el-form-item>
          </el-col>
          <el-col :span="12">
              <el-form-item label="上报时间" prop="chkTime"  >
                  <el-select size="small" @focus="timeFocus" v-model="form.chkTime" placeholder="选择上报时间" >
                      <el-option v-for="dict in timeList" :key="dict" :label="dict.stName" :value="dict"/>
                  </el-select>
              </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="btntype=='update'" >
          <el-col :span="12" >
              <el-form-item label="测站" prop="statCd"  >
                  <el-select v-model="form.statCd" placeholder="请选择测站" filterable size="small" disabled style="width: 100%;" >
                      <el-option v-for="dict in czlist" :key="dict.statCd"
                          :label="dict.statNm" :value="dict.statCd" />
                  </el-select>
              </el-form-item>
          </el-col>
          <el-col :span="12" >
              <el-form-item label="上报时间" prop="chkTime"  >
                  <el-input v-model="form.chkTime" disabled />
              </el-form-item>
          </el-col>
        </el-row>
        <el-row>
            <el-col :span="12">
                <el-form-item label="即时水位" prop="curLevel">
                    <el-input v-model="form.curLevel" placeholder="请输入即时水位" />
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="瞬时流量" prop="curFlow">
                    <el-input v-model="form.curFlow" placeholder="请输入瞬时流量" />
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="累计流量" prop="sumFlow">
                    <el-input v-model="form.sumFlow" placeholder="请输入累计流量" />
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="观测人员" prop="opeMan">
                    <el-input v-model="form.opeMan" placeholder="请输入观测人员" />
                </el-form-item>
            </el-col>
            <!-- <el-col :span="12">
              <div style="width: 200px; height: 10px;" ></div>
            </el-col> -->
            <el-col :span="12">
                <el-form-item label="记录人" prop="putMan">
                    <el-input v-model="form.putMan" placeholder="请输入记录人" />
                </el-form-item>
            </el-col>
            <el-col :span="24">
                <el-form-item label="备注" prop="statMark">
                    <el-input type="textarea" :autosize="{ minRows: 2, maxRows: 4}" v-model="form.statMark" placeholder="请输入备注" />
                </el-form-item>
            </el-col>
        </el-row>




        <!-- <el-form-item label="记录时间" prop="putTime">
          <el-date-picker clearable
            v-model="form.putTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择记录时间">
          </el-date-picker>
        </el-form-item> -->


      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listStManstatR, getStManstatR_new, delStStationR_new, addStManstatR, updateStManstatR, selMissingTime } from "@/api/waterManagement/StManstatR";
import { AllListWater } from "@/api/datam/StStationB";

export default {
  name: "StManstatR",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 水情人工观测站数据记录表格数据
      StManstatRList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        curLevel: null,
        curFlow: null,
        sumFlow: null,
        opeMan: null,
        putTime: null,
        putMan: null,
        statMark: null,
        statCd:null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
          curLevel: [{ required: true, trigger: "blur", message: "请输入即时水位" }],
          curFlow: [{ required: true, trigger: "blur", message: "请输入瞬时流量" }],
          sumFlow: [{ required: true, trigger: "blur", message: "请输入累计流量" }],
          statCd:[{ required: true, message: '请选择测站', trigger: 'change' } ],
          dt: [{ required: true, trigger: "blur", message: "请选择日期" }],
          chkTime: [{ required: true, trigger: "change", message: "请选择上报时间" }],
      },
      czlist:[],
      timeList:[],
      btntype:''
    };
  },
  created() {
    this.loadczlist();
    this.queryParams.chkTime = this.getNowDate()
  },
  methods: {
    loadczlist(){
      AllListWater({statType:4}).then(response => {
            let clist = response.rows;
            this.czlist = clist;
            if(clist.length>0){
              this.queryParams.statCd = clist[0].statCd;
              this.getList();
            }
        });
    },
    gettime(){
        if(!this.form.dt || !this.form.statCd){
            return
        }
        selMissingTime({
            statCd:this.form.statCd,
            chkTime:this.form.dt,
        }).then(res => {
            this.form.chkTime = '';
            this.timeList = res.data
        });
    },
    timeFocus(){
      if(!this.form.dt || !this.form.statCd){
        this.$message({
          message: '请先选择日期和测站',
          type: 'error'
        });
        return
      }

    },
    /** 查询水情人工观测站数据记录列表 */
    getList() {
      this.loading = true;
      listStManstatR(this.queryParams).then(response => {
        this.StManstatRList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        statCd: null,
        chkTime: null,
        dt: null,
        curLevel: null,
        curFlow: null,
        sumFlow: null,
        opeMan: null,
        putTime: null,
        putMan: null,
        statMark: null
      };
      this.btntype = '';
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {

      this.queryParams.pageNum = 1;
      console.log(this.queryParams,999999)
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.statCd)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.btntype = 'add';
      this.open = true;
      this.title = "添加水情人工观测站数据记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.btntype = 'update';
      const statCd = row.statCd;
      const chkTime = row.chkTime;
      let pd = {
        statCd: statCd,
        chkTime: chkTime,
      }
      getStManstatR_new(pd).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改水情人工观测站数据记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.btntype=='update') {
            updateStManstatR(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else if(this.btntype=='add') {
            addStManstatR(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const statCd = row.statCd;
      const chkTime = row.chkTime;
      this.$modal.confirm('是否确认删除水情人工观测站数据记录数据项？').then(function() {
        let dpd = {
          statCd: statCd,
          chkTime: chkTime,
        }
        return delStStationR_new(dpd);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('waterManagement/StManstatR/export', {
        ...this.queryParams
      }, `StManstatR_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
